package com.ruoyi.web.controller.course;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.TCourseUser;
import com.ruoyi.course.service.ITCourseUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程信息连接Controller
 *
 * @author Bandit
 * @date 2022-11-01
 */
@RestController
@RequestMapping("/course/CourseUser")
public class TCourseUserController extends BaseController
{
    @Autowired
    private ITCourseUserService tCourseUserService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询课程信息连接列表
     */
    @PreAuthorize("@ss.hasPermi('course:CourseUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCourseUser tCourseUser)
    {
        startPage();
        List<TCourseUser> list = tCourseUserService.selectTCourseUserList(tCourseUser);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('course:CourseUser:list')")
    @GetMapping("/stuList")
    public TableDataInfo stuList(Long courseId)
    {
        startPage();
        List<SysUser> studentList = userService.selectStudentListByCourseId(courseId);
        return getDataTable(studentList);
    }

    @GetMapping("/unChosenList")
    public AjaxResult unChosenList(@RequestParam("courseId") Long courseId)
    {
        if (courseId == null) {
            return AjaxResult.error("courseId 为空");
        }
        List<SysUser> studentList = userService.selectUnChosenListByCourseId(courseId);
        return AjaxResult.success(studentList);
    }

    /**
     * 导出课程信息连接列表
     */
    @PreAuthorize("@ss.hasPermi('course:CourseUser:export')")
    @Log(title = "课程信息连接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCourseUser tCourseUser)
    {
        List<TCourseUser> list = tCourseUserService.selectTCourseUserList(tCourseUser);
        ExcelUtil<TCourseUser> util = new ExcelUtil<TCourseUser>(TCourseUser.class);
        util.exportExcel(response, list, "课程信息连接数据");
    }

    /**
     * 获取课程信息连接详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:CourseUser:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    {
        return success(tCourseUserService.selectTCourseUserByCourseId(courseId));
    }

    /**
     * 新增课程信息连接
     */
    @PreAuthorize("@ss.hasPermi('course:CourseUser:add')")
    @Log(title = "课程信息连接", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCourseUser tCourseUser)
    {
        return toAjax(tCourseUserService.insertTCourseUser(tCourseUser));
    }

    @PreAuthorize("@ss.hasPermi('course:CourseUser:add')")
    @Log(title = "课程信息连接", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addBatch(@RequestBody List<TCourseUser> courseUserList)
    {
        return toAjax(tCourseUserService.insertTCourseUserBatch(courseUserList));
    }

    /**
     * 修改课程信息连接
     */
    @PreAuthorize("@ss.hasPermi('course:CourseUser:edit')")
    @Log(title = "课程信息连接", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCourseUser tCourseUser)
    {
        return toAjax(tCourseUserService.updateTCourseUser(tCourseUser));
    }

    /**
     * 删除课程信息连接
     */
    @PreAuthorize("@ss.hasPermi('course:CourseUser:remove')")
    @Log(title = "课程信息连接", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds)
    {
        return toAjax(tCourseUserService.deleteTCourseUserByCourseIds(courseIds));
    }

    /**
     * 删除课程信息连接
     */
    @PreAuthorize("@ss.hasPermi('course:CourseUser:remove')")
    @Log(title = "课程信息连接", businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(@RequestBody List<TCourseUser> courseUserList)
    {
        return toAjax(tCourseUserService.deleteTCourseUserByCourseList(courseUserList));
    }
}
